package com.internal.SharedModule.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.internal.Internalhibernate.client.MlImage;
import com.internal.Internalhibernate.server.dao.MlImageDAO;

/**
 * Servlet implementation class ImageViewServlet
 */
public class ImageViewServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ImageViewServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String imageid = request.getParameter("imageid");

		MlImageDAO imageDAO = new MlImageDAO();
		MlImage mlImage = imageDAO.findById(Long.parseLong(imageid));

		byte[] bs = mlImage.getImageFile();

		if (bs != null && bs.length > 0) {
			response.setContentType("image/png");
			response.getOutputStream().write(bs);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
